package cn.echoedu.model;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.Student;
import cn.echoedu.entity.receivecommand.StudentListReceive;
import cn.echoedu.util.L;

public class StudentModel extends BaseModel{
	private final String T="StudentControl";

	public StudentModel(){

	}
	public Student queryByUserNumberAndPassword(String user_number,
			String password) throws SQLException {
		// TODO Auto-generated method stub
		L.p(T+"queryByUserNumberAndPassword");

		String sql="select * from student left join class on (student.classcode=class.classcode)" 
				+"left join  professional on (class.professionalcode=professional.professionalcode)"
				+"left join department on (professional.departmentcode=department.departmentcode)"
				+"left join school on (department.schoolcode=school.schoolcode)"
				+"left join job on (student.jobcode=job.jobcode)"
				+"left join image on (student.imageid=image.imageid)"
				+"left join degress on (student.degresscode=degress.degressid)"
				+"left join politicalstatus on (student.politicalstatuscode=politicalstatus.politicalstatuscode)"
				+"where studentnumber='"+user_number+"' and password ='"+password+"'";
		List<Student> studentlist=getDAORead().getPojoForSql(getConnection(),
				TableName.STUDENT_TABLE,sql);
		if(studentlist!=null&&studentlist.size()>0){
			return studentlist.get(0);
		}
		return null;
	}
	public boolean saveUserRunning(Student student) {
		// TODO Auto-generated method stub
		try {
			return getDAOInsert().putInto(getConnection(), TableName.STUDENT_TABLE,
					student, false);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateUserName(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserName*********>>";
		L.p(T+M);
		String sql = "update "+TableName.STUDENT_TABLE+" set name='"+student.getName()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);
	}
	public boolean updateUserHeadImage(Student student) {
		// TODO Auto-generated method stub
		final String M = "updateUserHeadImage********>>";
		String sql="update "+TableName.STUDENT_TABLE+" set imageid='"
				+student.getImage().getImageid()+"' where studentnumber='"
				+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);
	}
	public boolean updateUserRealName(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserRealName*******>>";
		String sql="update "+TableName.STUDENT_TABLE+" set realname='"+student.getRealname()
				+"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserPoliticalstatus(Student student) {
		// TODO Auto-generated method stub
		final String M = "updateUserPoliticalstatus******>>";
		String sql="update "+TableName.STUDENT_TABLE+" set politicalstatuscode='"
				+student.getPoliticalstatus().getPoliticalstatuscode()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserDegress(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserDegress***********>>";
		String sql = "update student set degresscode='"
				+student.getDegress().getDegressid()
				+"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(), sql);

	}
	public boolean updateUserJob(Student student) {
		// TODO Auto-generated method stub
		String sql="";
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserClassName(Student student) {
		// TODO Auto-generated method stub
		final String M = "updateUserClassName********>>";
		String sql="update student set classcode='"
				+student.getAclass().getClasscode()+"' where studentnumber='"
				+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserWechat(Student student) {
		// TODO Auto-generated method stub
		final String M="upateUserWechat**********>>";
		String sql="update student set wechatnumber='"+student.getWechatnumber()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserSina(Student student) {
		// TODO Auto-generated method stub
		String sql="update student set sinanumber='"+student.getSinanumber()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserBaiDu(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserBaiDu**********>>";
		String sql="update student set baidunumber='"+student.getBaidunumber()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserQQ(Student student) {
		// TODO Auto-generated method stub
		String sql="update student set qq='"+student.getQq()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserPassword(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserPassword*********>>";
		String sql="update student set password='"+student.getPassword()+
				"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserEntranceTime(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserEntranceTime*********>>";
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd");
		String sql="update student set entrancetime ='"
				+dateformat.format(student.getEntrancetime())
				+"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);

	}
	public boolean updateUserBirthday(Student student) {
		// TODO Auto-generated method stub
		final String M="updateUserBirthday********>>";
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-mm-dd");
		String sql="update student set birthday='"
				+dateformat.format(student.getBirtyday())
				+"' where studentnumber='"+student.getStudentnumber()+"'";
		L.p(T+M+sql);
		return getDAOUpdate().updateByColumn(getConnection(),sql);
	}
	public boolean updateUserSex(Student student) {
		// TODO Auto-generated method stub
		String sql="update student set sex='"+student.getSex()
				+"' where studentnumber='"+student.getStudentnumber()+"'";
		return getDAOUpdate().updateByColumn(getConnection(),sql);
	}

	public List<Student> queryStudentListByClassCode(int classcode) {
		// TODO Auto-generated method stub
		List<Student> studentlist = null;
		try {
			studentlist = getDAORead().getAllForInput(getConnection(),
					TableName.STUDENT_TABLE,"classcode",classcode+"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return studentlist;
	}
	public List<Student> querySchoolInfoByUserNumber(String user_number) {
		// TODO Auto-generated method stub
		String sql = " select * from student left join class on student.classcode"
				+" = class.classcode"
				+" left join professional on professional.professionalcode =" 
				+" class.professionalcode"
				+" left join department on department.departmentcode = " 
				+"professional.departmentcode"
				+" left join school on school.schoolcode = department.schoolcode"
				+" where studentnumber ='"+user_number+"'";
		try {
			return getDAORead().getPojoForSql(getConnection(),TableName.STUDENT_TABLE,
					sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
